package com.controller.system.agent;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.system.SystemGlobalConfig;
import com.service.system.agent.SystemGlobalConfigService;


@Controller
@RequestMapping("/agentConfig")
public class AgentConfigController  {
	
	@Autowired
	SystemGlobalConfigService systemGlobalConfigService;
	
	@RequestMapping(value = "/findAgentConfingList", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> findAgentConfingList(HttpServletRequest request){
		
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String configName = request.getParameter("configName");
		
		Map<String, String> paraMap = new HashMap<>();
		paraMap.put("configGroup", "agent");
		paraMap.put("order", "create_time desc");
		paraMap.put("pageNum", pageNum);
		paraMap.put("pageSize", pageSize);
		paraMap.put("configName", configName);
		Map<String, Object> agentConfigMap = systemGlobalConfigService.findAgentConfigList(paraMap);
		return agentConfigMap;
	}
	
	
	@RequestMapping(value = "/findAgentConfigById", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> findAgentConfigById(HttpServletRequest request){
		
		Map<String, Object> map = new HashMap<>();
		int id = Integer.parseInt(request.getParameter("id"));
		SystemGlobalConfig agentConfig = systemGlobalConfigService.findAgentConfigById(id);
		map.put("agentConfig", agentConfig);
		return map;
	}
	
	
	@RequestMapping(value = "/updateAgentConfigById", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> updateAgentConfigById(HttpServletRequest request){
		
		Map<String, Object> map = new HashMap<>();
		
		String id = request.getParameter("configId");
		String name = request.getParameter("configName");
		String content = request.getParameter("configContent");
		
		SystemGlobalConfig agentConfig = new SystemGlobalConfig();
		agentConfig.setId(Integer.parseInt(id));
		agentConfig.setConfigName(name);
		agentConfig.setConfigContent(content);
		
		int row = systemGlobalConfigService.updateAgentConfig(agentConfig);
		
		if(row == 1) {
			map.put("code", 1);
			map.put("message", "更新成功");
		}else {
			map.put("code", 0);
			map.put("message", "更新失败");
		}
		
		return map;
	}
	
	
}